package com.stylefeng.guns.rest.modular.film.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.api.film.FilmServiceApi;
import com.stylefeng.guns.api.film.vo.*;
import com.stylefeng.guns.core.util.DateUtil;
import com.stylefeng.guns.rest.common.persistence.dao.*;
import com.stylefeng.guns.rest.common.persistence.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author: jiaru
 * @Description: 电影接口默认实现
 * @Date: Created on 2019/3/23
 * @Modify:
 */
@Component
@Service(interfaceClass = FilmServiceApi.class)
public class DefaultFilmServiceImpl implements FilmServiceApi {

    @Autowired
    private MeetBannerMapper meetBannerMapper;

    @Autowired
    private MeetFilmMapper meetFilmMapper;

    @Autowired
    private MeetCatDictMapper meetCatDictMapper;

    @Autowired
    private MeetYearDictMapper meetYearDictMapper;

    @Autowired
    private MeetSourceDictMapper meetSourceDictMapper;

    @Autowired
    private MeetFilmInfoMapper meetFilmInfoMapper;

    @Autowired
    private MeetActorMapper meetActorMapper;

    @Override
    public List<BannerVo> getBanners() {
        List<BannerVo> result = new ArrayList<>();
        List<MeetBanner> meetBanners = meetBannerMapper.selectList(null);

        for (MeetBanner meetBanner : meetBanners) {
            BannerVo bannerVo = new BannerVo();
            bannerVo.setBannerId(meetBanner.getUuid() + "");
            bannerVo.setBannerUrl(meetBanner.getBannerUrl());
            bannerVo.setBannerAddress(meetBanner.getBannerAddress());
            result.add(bannerVo);
        }

        return result;
    }

    private List<FilmInfo> getFilmInfos(List<MeetFilm> meetFilms) {
        List<FilmInfo> filmInfos = new ArrayList<>();
        for (MeetFilm meetFilm : meetFilms) {
            FilmInfo filmInfo = new FilmInfo();
            filmInfo.setScore(meetFilm.getFilmScore());
            filmInfo.setImgAddress(meetFilm.getImgAddress());
            filmInfo.setFilmType(meetFilm.getFilmType());
            filmInfo.setFilmScore(meetFilm.getFilmScore());
            filmInfo.setFilmName(meetFilm.getFilmName());
            filmInfo.setFilmId(meetFilm.getUuid() + "");
            filmInfo.setExpectNum(meetFilm.getFilmPresalenum());
            filmInfo.setBoxNum(meetFilm.getFilmBoxOffice());
            filmInfo.setShowTime(DateUtil.getDay(meetFilm.getFilmTime()));

            // 将转换的对象放入结果集
            filmInfos.add(filmInfo);
        }

        return filmInfos;
    }

    @Override
    public FilmVo getHotFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmVo filmVo = new FilmVo();
        List<FilmInfo> filmInfos = new ArrayList<>();

        // 热映影片的限制条件
        EntityWrapper<MeetFilm> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", "1");
        // 判断是否是首页需要的内容
        if (isLimit) {
            // 如果是，则限制条数、限制内容为热映影片
            Page<MeetFilm> page = new Page<>(1, nums);
            List<MeetFilm> meetFilms = meetFilmMapper.selectPage(page, entityWrapper);
            // 组织filmInfos
            filmInfos = getFilmInfos(meetFilms);
            filmVo.setFilmNum(meetFilms.size());
            filmVo.setFilmInfo(filmInfos);
        } else {
            // 如果不是，则是列表页，同样需要限制内容为热映影片
            Page<MeetFilm> page = null;
            // 根据sortId的不同，来组织不同的Page对象
            // 1-按热门搜索，2-按时间搜索，3-按评价搜索
            switch (sortId) {
                case 1:
                    page = new Page<>(nowPage, nums, "film_box_office");
                    break;
                case 2:
                    page = new Page<>(nowPage, nums, "film_time");
                    break;
                case 3:
                    page = new Page<>(nowPage, nums, "film_score");
                    break;
                default:
                    page = new Page<>(nowPage, nums, "film_box_office");
                    break;
            }

            // 如果sourceId,yearId,catId 不为99 ,则表示要按照对应的编号进行查询
            if (sourceId != 99) {
                entityWrapper.eq("film_source", sourceId);
            }
            if (yearId != 99) {
                entityWrapper.eq("film_date", yearId);
            }
            if (catId != 99) {
                // #2#4#22#
                String catStr = "%#" + catId + "#%";
                entityWrapper.like("film_cats", catStr);
            }

            List<MeetFilm> meetFilms = meetFilmMapper.selectPage(page, entityWrapper);
            // 组织filmInfos
            filmInfos = getFilmInfos(meetFilms);
            filmVo.setFilmNum(meetFilms.size());

            // 需要总页数 totalCounts/nums -> 0 + 1 = 1
            // 每页10条，我现在有6条 -> 1
            int totalCounts = meetFilmMapper.selectCount(entityWrapper);
            int totalPages = (totalCounts / nums) + 1;

            filmVo.setFilmInfo(filmInfos);
            filmVo.setTotalPage(totalPages);
            filmVo.setNowPage(nowPage);
        }

        return filmVo;
    }

    @Override
    public FilmVo getSoonFilms(boolean isLimit, int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmVo filmVo = new FilmVo();
        List<FilmInfo> filmInfos = new ArrayList<>();

        // 即将上映影片的限制条件
        EntityWrapper<MeetFilm> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", "2");
        // 判断是否是首页需要的内容
        if (isLimit) {
            // 如果是，则限制条数、限制内容为热映影片
            Page<MeetFilm> page = new Page<>(1, nums);
            List<MeetFilm> meetFilms = meetFilmMapper.selectPage(page, entityWrapper);
            // 组织filmInfos
            filmInfos = getFilmInfos(meetFilms);
            filmVo.setFilmNum(meetFilms.size());
            filmVo.setFilmInfo(filmInfos);
        } else {
            // 如果不是，则是列表页，同样需要限制内容为即将上映影片
            Page<MeetFilm> page = null;
            // 根据sortId的不同，来组织不同的Page对象
            // 1-按热门搜索，2-按时间搜索，3-按评价搜索
            switch (sortId) {
                case 1:
                    page = new Page<>(nowPage, nums, "film_preSaleNum");
                    break;
                case 2:
                    page = new Page<>(nowPage, nums, "film_time");
                    break;
                case 3:
                    page = new Page<>(nowPage, nums, "film_preSaleNum");
                    break;
                default:
                    page = new Page<>(nowPage, nums, "film_preSaleNum");
                    break;
            }

            // 如果sourceId,yearId,catId 不为99 ,则表示要按照对应的编号进行查询
            if (sourceId != 99) {
                entityWrapper.eq("film_source", sourceId);
            }
            if (yearId != 99) {
                entityWrapper.eq("film_date", yearId);
            }
            if (catId != 99) {
                // #2#4#22#
                String catStr = "%#" + catId + "#%";
                entityWrapper.like("film_cats", catStr);
            }

            List<MeetFilm> meetFilms = meetFilmMapper.selectPage(page, entityWrapper);
            // 组织filmInfos
            filmInfos = getFilmInfos(meetFilms);
            filmVo.setFilmNum(meetFilms.size());

            // 需要总页数 totalCounts/nums -> 0 + 1 = 1
            // 每页10条，我现在有6条 -> 1
            int totalCounts = meetFilmMapper.selectCount(entityWrapper);
            int totalPages = (totalCounts / nums) + 1;

            filmVo.setFilmInfo(filmInfos);
            filmVo.setTotalPage(totalPages);
            filmVo.setNowPage(nowPage);
        }

        return filmVo;
    }

    @Override
    public FilmVo getClassicFilms(int nums, int nowPage, int sortId, int sourceId, int yearId, int catId) {
        FilmVo filmVo = new FilmVo();
        List<FilmInfo> filmInfos = new ArrayList<>();

        // 即将上映影片的限制条件
        EntityWrapper<MeetFilm> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", "3");

        // 如果不是，则是列表页，同样需要限制内容为即将上映影片
        Page<MeetFilm> page = null;
        // 根据sortId的不同，来组织不同的Page对象
        // 1-按热门搜索，2-按时间搜索，3-按评价搜索
        switch (sortId) {
            case 1:
                page = new Page<>(nowPage, nums, "film_box_office");
                break;
            case 2:
                page = new Page<>(nowPage, nums, "film_time");
                break;
            case 3:
                page = new Page<>(nowPage, nums, "film_score");
                break;
            default:
                page = new Page<>(nowPage, nums, "film_box_office");
                break;
        }

        // 如果sourceId,yearId,catId 不为99 ,则表示要按照对应的编号进行查询
        if (sourceId != 99) {
            entityWrapper.eq("film_source", sourceId);
        }
        if (yearId != 99) {
            entityWrapper.eq("film_date", yearId);
        }
        if (catId != 99) {
            // #2#4#22#
            String catStr = "%#" + catId + "#%";
            entityWrapper.like("film_cats", catStr);
        }

        List<MeetFilm> meetFilms = meetFilmMapper.selectPage(page, entityWrapper);
        // 组织filmInfos
        filmInfos = getFilmInfos(meetFilms);
        filmVo.setFilmNum(meetFilms.size());

        // 需要总页数 totalCounts/nums -> 0 + 1 = 1
        // 每页10条，我现在有6条 -> 1
        int totalCounts = meetFilmMapper.selectCount(entityWrapper);
        int totalPages = (totalCounts / nums) + 1;

        filmVo.setFilmInfo(filmInfos);
        filmVo.setTotalPage(totalPages);
        filmVo.setNowPage(nowPage);

        return filmVo;
    }


    @Override
    public List<FilmInfo> getBoxRanking() {
        // 条件 -> 正在上映的，票房前10名
        EntityWrapper<MeetFilm> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", "1");

        Page<MeetFilm> page = new Page<>(1, 10, "film_box_office");

        List<MeetFilm> meetFilms = meetFilmMapper.selectPage(page, entityWrapper);

        List<FilmInfo> filmInfos = getFilmInfos(meetFilms);

        return filmInfos;
    }

    @Override
    public List<FilmInfo> getExpectRanking() {
        // 条件 -> 即将上映的，预售前10名
        EntityWrapper<MeetFilm> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", "2");

        Page<MeetFilm> page = new Page<>(1, 10, "film_preSaleNum");

        List<MeetFilm> meetFilms = meetFilmMapper.selectPage(page, entityWrapper);

        List<FilmInfo> filmInfos = getFilmInfos(meetFilms);

        return filmInfos;

    }

    @Override
    public List<FilmInfo> getTop() {
        // 条件 -> 正在上映的，评分前10名
        EntityWrapper<MeetFilm> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("film_status", "1");

        Page<MeetFilm> page = new Page<>(1, 10, "film_score");

        List<MeetFilm> meetFilms = meetFilmMapper.selectPage(page, entityWrapper);

        List<FilmInfo> filmInfos = getFilmInfos(meetFilms);

        return filmInfos;
    }

    @Override
    public List<CatVo> getCats() {
        List<CatVo> cats = new ArrayList<>();
        // 查询实体对象 - MeetCatDictT
        List<MeetCatDict> meetCats = meetCatDictMapper.selectList(null);
        // 将实体对象转换为业务对象 - CatVo
        for (MeetCatDict meetCatDict : meetCats) {
            CatVo catVo = new CatVo();
            catVo.setCatId(meetCatDict.getUuid() + "");
            catVo.setCatName(meetCatDict.getShowName());

            cats.add(catVo);
        }

        return cats;
    }

    @Override
    public List<SourceVo> getSources() {
        List<SourceVo> sources = new ArrayList<>();
        List<MeetSourceDict> meetSourceDicts = meetSourceDictMapper.selectList(null);
        for (MeetSourceDict meetSourceDict : meetSourceDicts) {
            SourceVo sourceVo = new SourceVo();

            sourceVo.setSourceId(meetSourceDict.getUuid() + "");
            sourceVo.setSourceName(meetSourceDict.getShowName());

            sources.add(sourceVo);
        }
        return sources;
    }

    @Override
    public List<YearVo> getYears() {
        List<YearVo> years = new ArrayList<>();
        // 查询实体对象 - MeetCatDictT
        List<MeetYearDict> meetYears = meetYearDictMapper.selectList(null);
        // 将实体对象转换为业务对象 - CatVo
        for (MeetYearDict meetYearDict : meetYears) {
            YearVo yearVo = new YearVo();
            yearVo.setYearId(meetYearDict.getUuid() + "");
            yearVo.setYearName(meetYearDict.getShowName());

            years.add(yearVo);
        }
        return years;
    }

    @Override
    public FilmDetailVo getFilmDetail(int searchType, String searchParam) {
        // searchType 1-按名称  2-按ID的查找
        if (searchType == 1) {
            return meetFilmMapper.getFilmDetailByName("%" + searchParam + "%");
        }

        return meetFilmMapper.getFilmDetailById(searchParam);
    }

    private MeetFilmInfo getFilmInfo(String filmId) {

        MeetFilmInfo meetFilmInfo = new MeetFilmInfo();
        meetFilmInfo.setFilmId(filmId);

        meetFilmInfo = meetFilmInfoMapper.selectOne(meetFilmInfo);

        return meetFilmInfo;
    }


}
